﻿
Imports System.ComponentModel 
Imports System.Collections  

Public Class CTFondosDistribLiq 
Implements INotifyPropertyChanged

   'Columnas sólo lectura
    'Fondos
    Private _CodFondo              As String
    Private _SalDisponible         As decimal       
    Private _OrdenPend             As decimal
    Private _PendingCash           As decimal
    Private _PendingCashAjust      As decimal
    Private _SaldoDecalogT         As decimal
    Private _SaldoDecalogCalculado As decimal  'Columana calculada

    Private _CargoComision               As Decimal = 0 
    Private _AbonoComision               As Decimal = 0 

    Private MontoTotalDistrib      As Decimal

    Private MontoToleranciaMinRemXFondo As Decimal = 0 
    


    'Columnas editadas 
    'Intermediarios    
    Private _Montointermediario          As decimal  
    Private _MontointermediarioInicial   As decimal         
    

    Public const INDEXINICIOINTERMEDIARIO As Integer = 7
    Public const INDEXFININTERMEDIARIO As Integer = 7
  


    Public Event PropertyChanged As PropertyChangedEventHandler  Implements INotifyPropertyChanged.PropertyChanged 




    Public Sub New(ByVal codFondo As String, ByVal salDisponible As decimal, ByVal ordenPend As decimal,  _
                   ByVal pendingCash As decimal,  ByVal PendingCashAjust As decimal, ByVal  saldoDecalogT  As decimal, _
                   ByRef Distribuido As CTDistribLiqReduc, _
                   ByVal MontoIntermediario As Decimal,ByVal Optional Toleranciarem As Decimal= 0, Optional pCargoComision As Decimal = 0, Optional pAbonoComision As Decimal=0)




    'Fondos 

    _codFondo         = codFondo
    _salDisponible    = salDisponible 
    _ordenPend        = ordenPend
    _pendingCash      = pendingCash
    _PendingCashAjust = PendingCashAjust
    _saldoDecalogT    = saldoDecalogT


    'Intermediarios

    _Montointermediario    = MontoIntermediario
    _MontointermediarioInicial = MontoIntermediario
 

    'Campo calculado
       

     _CargoComision = pCargoComision
     _AbonoComision = pAbonoComision


     MontoToleranciaMinRemXFondo = Toleranciarem
     MontoTotalDistrib = Distribuido.ObtenerImporteTotalDistribXFondo (_codFondo)
     _saldoDecalogCalculado = (_saldoDecalogT-Toleranciarem - _CargoComision + _AbonoComision) - MontoTotalDistrib 

    
    End Sub


   
    Public Shared Function  ObtenerPosiciones(ByVal nomPropiedad As String ) As Integer 
      
         Select Case nomPropiedad 

                Case "CodFondo"
                    Return 0
                Case "SalDisponible"
                    Return 1
                Case "OrdenPend"
                    Return 2
                Case "PendingCash"
                    Return 3
                Case "PendingCashAjust"
                    Return 4
                Case "SaldoDecalogT"
                    Return 5
                Case "SaldoDecalogCalculado"
                    Return 6
                Case "MontoIntermediario"
                    Return 7  
                    
                Case "CargoComision"    
                    Return 8
                Case "AbonoComision"    
                    Return 9                                 
                Case ELSE
                    Return -1
             End Select
      
    End Function


    Public Shared Function ValorInicalColumna(ByVal index As Integer) As Object     
        
         Select Case index

                Case 0 'CodFondo
                     Return ""
                Case 1 'SalDisponible
                     Return 0
                Case 2 'OrdenPend
                     Return 0
                Case 3 'PendingCash
                     Return 0
                Case 4 'PendingCashAjust
                     Return 0
                Case 5 'SaldoDecalogT
                     Return 0                                   
                Case 6 'SaldoDecalogCalculado
                     Return 0                     
                Case 7 'Intermediario1
                     Return 0          
                     
                Case 8
                     Return 0
                Case 9
                     Return 0

                                                            
                Case Else 
                     Return Nothing            
            End Select


        
    End Function
    
    
    Private Sub NotificarCambio (propname As string  )
     RaiseEvent PropertyChanged(me, new PropertyChangedEventArgs(propname) )
    End Sub


    Default   Property  Columna(ByVal index As Integer) As Object
        Get                                                                                          
             Select Case index 

                Case 0
                    Return _codFondo
                Case 1
                    Return _salDisponible
                Case 2
                    Return _ordenPend
                Case 3
                    Return _pendingCash
                Case 4
                    Return _pendingCashAjust
                Case 5
                    Return _saldoDecalogT
                Case 6
                    Return _saldoDecalogCalculado
                Case 7
                    Return _Montointermediario  
                
                Case 8
                    Return _CargoComision     
                Case 9
                    Return _AbonoComision
                                   
                Case ELSE
                    Return Nothing 
             End Select


        End Get
        Set(ByVal value As Object )
           
            Select Case index 

                Case 0
                    CodFondo  = value 
                Case 1
                     SalDisponible = value
                Case 2
                     OrdenPend = value
                Case 3
                     PendingCash = value
                Case 4
                     PendingCashAjust = value
                Case 5
                     SaldoDecalogT = value
                Case 6
                     _saldoDecalogCalculado = FormulaSalMenosInter ()
                Case 7
                      
                           
                        If   value > 0 then 
                          _Montointermediario = value                                              
                        Else
                          _Montointermediario = 0                                                                              
                        End If                         

                        NotificarCambio("MontoIntermediario")    
    
                        _saldoDecalogCalculado = FormulaSalMenosInter()
                        NotificarCambio ("SaldoDecalogCalculado")                                              

             End Select



        End Set

    End Property


    #Region "Bloque de solo lectura"

  


    Public Property CodFondo() As String
        Get 
            Return _codFondo
        End Get
        Set(ByVal value As String)
            _codFondo = value               
        End Set
    End Property


    Public Property SalDisponible() As decimal   
        Get
            Return _salDisponible
        End Get
        Set(ByVal value As decimal)
            _salDisponible = value        
            NotificarCambio("SalDisponible")
        End Set
    End Property


    Public Property OrdenPend() As decimal
        Get
            Return _ordenPend 
        End Get
        Set(ByVal value As decimal)
            _ordenPend = value        
            NotificarCambio("OrdenPend")
        End Set
    End Property


    Public Property PendingCash() As decimal
        Get
            Return _pendingCash
        End Get
        Set(ByVal value As decimal)
            _pendingCash = value        
            NotificarCambio("PendingCash")
        End Set
    End Property


    Public Property PendingCashAjust() As decimal
        Get
            Return _pendingCashAjust
        End Get
        Set(ByVal value As decimal)
            _pendingCashAjust = value        
            NotificarCambio("PendingCashAjust")
        End Set
    End Property


    Public Property SaldoDecalogT() As decimal
        Get
            Return _saldoDecalogT
        End Get
        Set(ByVal value As decimal )
            _saldoDecalogT = value
            NotificarCambio("SaldoDecalogT")
        End Set

    End Property


    Public ReadOnly  Property SaldoDecalogCalculado() As decimal
        Get
            Return _saldoDecalogCalculado
        End Get
        
    End Property


    #End Region


    #Region "Bloque capturable" 

    Public Property MontoIntermediario() As decimal 
        Get
            Return _Montointermediario 
        End Get
        Set(ByVal value As decimal)
            
               If   value > 0 then 
                   _Montointermediario = value                                              
                Else
                   _Montointermediario = 0                                                                              
               End If                         

               NotificarCambio("MontoIntermediario")    
    
             _saldoDecalogCalculado = FormulaSalMenosInter()
              NotificarCambio ("SaldoDecalogCalculado") 
        End Set


    End Property

    #End Region



    
    Public ReadOnly Property CargoComision() As Decimal
        Get
            Return _CargoComision
        End Get
        
    End Property

    Public ReadOnly Property AbonoComision() As Decimal
        Get
            Return _AbonoComision
        End Get
        
    End Property



    Private Function  FormulaSalMenosInter() As Double    
      Return (_saldoDecalogT - MontoToleranciaMinRemXFondo - _CargoComision + _AbonoComision ) - (MontoTotalDistrib -_MontointermediarioInicial  +  _Montointermediario  )
    End Function  

    
    
End Class




Public Class CTDistribLiqReduc


Private _IdPs                As long
Private _IdPEJ               As Long 
Private _LstDistrib          As List(Of structDistribFondoXInter )
Private _MontoTotalDistrib   As Decimal
Private _MontoRemanente      As Decimal 
Private _EsParcial           As Boolean 

Private _DistgribTotalXFondo As Decimal 

Public Structure structDistribFondoXInter   
 Public  CodFondo            As String
 Public  MontoDistrib        As Decimal 
End Structure





Public Sub new (ByVal pIdPS As Long, ByVal pIdPEJ As Long)

   _IdPs         = pIdPS 
   _IdPEJ        = pIdPEJ 
  
   _LstDistrib = New List(Of structDistribFondoXInter )

End Sub




Public Property IdPs() As Long
    Get
        Return _IdPs 
    End Get
    Set(ByVal value As Long)
        _IdPs  = value
    End Set
End Property

Public Property IdPEJ() As Long 
    Get
        Return _IdPEJ 
    End Get
    Set(ByVal value As Long)
        _IdPEJ = value
    End Set
End Property


Public Property LstDistrib() As List(Of structDistribFondoXInter )
    Get
        Return _LstDistrib
    End Get
    Set(ByVal value As List(Of structDistribFondoXInter ))
        _LstDistrib = value
    End Set
End Property


Public Property MontoTotalDistrib() As Decimal 
    Get
        Return _MontoTotalDistrib 
    End Get
    Set(ByVal value As Decimal )
        _MontoTotalDistrib = value
    End Set
End Property


Public ReadOnly Property MontoRemanente() As Decimal 
    Get
        Dim dis = (From fd As structDistribFondoXInter In _LstDistrib Select fd.MontoDistrib).Sum()
        _MontoRemanente = _MontoTotalDistrib - dis
        Return _MontoRemanente
    End Get
        
End Property

Public Property EsParcial() As Boolean 
    Get
        Return _EsParcial 
    End Get
    Set(ByVal value As Boolean)
        _EsParcial = value
    End Set
End Property






Public Function ObtenerImporteTotalDistribXFondo (ByVal CodFondo As String) As decimal 

 Dim lstAux As List(Of CTDistribLiqReduc.structDistribFondoXInter ) = Nothing 
 Dim MontoTotalDistrib As Decimal  = 0             

 if _LstDistrib.Count > 0 then 
    
  
       lstAux =  LstDistrib.FindAll (Function (x) x.CodFondo.Equals (CodFondo))
 
       If Not lstAux is Nothing then 
          For Each distribucion As CTDistribLiqReduc.structDistribFondoXInter  In lstAux 
              MontoTotalDistrib += distribucion.MontoDistrib  
          Next                             
       End If
       
    End If
   
   Return MontoTotalDistrib

End Function



End Class

